<?php
/**
 * Defines Kodform_Element_Select class
 *
 *
 * @copyright 	Copyright (c) 2006 Boris Tomic (kodmasin.net)
 * @category 	Kodform
 * @package 	Kodform
 * @author 		Boris Tomic
 * @license 	http://boris.kodmasin.net/kodform4:license The BSD License
 *
 */
require_once 'Kodform/Element.php';
require_once 'Kodform/Attributes.php';

/**
 * select element implementation
 */
class Kodform_Element_Select extends Kodform_Element{
	private $options;
	private $error;
	/**
	 * Creates select element
	 * 
	 *  @param string $id element idenftifer
	 *  @param string $displayName name which is used when displaying to user
	 *  @param $error this is error message if element value is not recognised.
	 *  When there is no oprion with this value. 
	 */
	function __construct($id, $displayName, $error, $multiselect = false){
		parent::__construct($id);
		self::setDisplayName($displayName);
		$this->options = array();
		$this->error = $error;
		$this->multi = $multiselect;
		if($this->multi)
			$this->defaultValue = array();
	}
	/**
	 * Overloaded function because this element does not use validators and filters.
	 * 
	 * It only checks if there is option with value same as POSTed value
	 */
	function validate(){
		$valid = true;
		//check if form is submited
		if($this->form != null && $this->form->isSubmited()){
			$this->rawValue = $this->form->getRaw($this->id);//get raw submited data
			//if select is multiselect
			if($this->multi){
				foreach($this->rawValue as $value){
					if(!$this->isInOptions($value)){
						$valid = false;
						array_push($this->errors, $this->error);
					}
				}
			}
			//if it is not mnultiselect
			else{
				if(!in_array($this->rawValue, $this->options)){
					$valid = false;
					array_push($this->errors, $this->error);
				}
			}
			if($valid)
				$this->validValue = $this->rawValue;
		}
		return $valid;
	}
	/**
	 * protected function which checks if value is in options
	 * 
	 * @param string $value value which will be checked
	 * @return boolean true if value is in options false
	 * if it is not*/
	protected function isInOptions($value){
		if(in_array($value, $this->options))
			return true;
		return false;
	}
	/**
	 * Adds new option
	 * 
	 * @param string $displayName name which will be wissivle to user
	 * @param string $value value of option
	 * @param boolena $default set this to true if this is defult element. 
	 * Default option will be displayed. If you add more then one default option 
	 * then last added will be default.
	 */
	function addOption($displayName, $value, $default = false){
		$this->options[$displayName] = $value;
		if($this->multi){
			if($default == true){
				array_push($this->defaultValue, $value);
			}
		}
		else
			if($default == true || count($this->options) == 1 )
				$this->defaultValue = $value;
	}
	/**
	 * Basic rendering of these element
	 */
	function display(Kodform_Attributes $attributes = null){
		if($attributes == null)
			$attributes = new Kodform_Attributes();
		$str = "<select id=\"" . $this->id;
		if($this->multi)
			$str .= "[]";
		$str .= "\" name=\"" . $this->id;
		if($this->multi)
			$str .= "[]";
		$str .= "\"";
		if($attributes == null)
				$attributes = new Kodform_Attributes();
			$attr = $attributes->normal;
			if(!$this->isValid())
				$attr = $attributes->error;
			$str .= $attr->display();
		if($this->multi == true)
			$str .= " multiple=\"multiple\"";
		$str .= ">";
		foreach($this->options as $optName => $optVal){
			$str .= "<option value=\"" . $optVal . "\"";
			if($this->multi){
				if(in_array($optVal, $this->getValue()))
					$str .= " selected=\"selected\"";
			}
			else{
				if($this->getValue() == $optVal)
					$str .= " selected=\"selected\"";
			}
			$str .= ">" . $optName . "</option>";
		}
		$str .="</select>";
		return $str; 
	}
	function getOptionName($value){
		return array_search($value, $this->options);
	}
}
